using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class KhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveKho(Kho kho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, kho.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenKho", DbType.String, ParameterDirection.Input, kho.TenKho);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all Kho in DB
		public ResultObj GetAllKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<Kho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<Kho>();
                Kho item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new Kho();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.TenKho = Convert.ToString(reader["TenKho"]);
                    lst.Add(item);
                }
            }
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all Kho in DB with condition
		public ResultObj GetAllKhoByID(Kho kho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<Kho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, kho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<Kho>();
			Kho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new Kho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenKho = Convert.ToString(reader["TenKho"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a Kho out of DB with condition
		public ResultObj DeleteKho(Kho kho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, kho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of CTKho from DB with condition
		public ResultObj GetCTKho(int IdCTKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetCTKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdCTKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTKho>();
			CTKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdKho = Convert.ToInt32(reader["IdKho"]);
            item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			item.SoLuongBanDau = Convert.ToInt64(reader["SoLuongBanDau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get info of QuanLyKho from DB with condition
		public ResultObj GetQuanLyKho(int IdQuanLyKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<QuanLyKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetQuanLyKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdQuanLyKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<QuanLyKho>();
			QuanLyKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new QuanLyKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdKho = Convert.ToInt32(reader["IdKho"]);
			item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			//item.SoChungTu = Convert.ToString(reader["SoChungTu"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
	}
}
